package com.example.service.impl;

import com.example.domain.dto.QuestionDTO;
import com.example.domain.po.AnswerRecord;
import com.example.domain.po.QuestionType;
import com.example.mapper.QuestionMapper;
import com.example.service.QuestionService;
import com.example.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<QuestionDTO> listByRandom() {
        return questionMapper.listByRandom();
    }

    @Override
    public void record(Map<String, Object> map) {
        Integer uid = (Integer) map.get("uid");
        String score =  String.valueOf(map.get("score")) ;
        questionMapper.record(uid,score);
    }

    @Override
    public List<AnswerRecord> listRecordByUid(Integer uid) {
        return questionMapper.listRecordByUid(uid);
    }

    @Override
    public List<QuestionDTO> listByQTypeId(Integer qTypeId) {
        return questionMapper.listByQTypeId(qTypeId);
    }

    @Override
    public PageBean<QuestionDTO> list(Integer pageNum, Integer pageSize, String topic, Integer type, Integer qtypeId) {
        PageBean<QuestionDTO> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);

        List<QuestionDTO> qList = questionMapper.list(topic,type,qtypeId);
        Page<QuestionDTO> p=(Page<QuestionDTO>)qList;

        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());

        return pb;
    }

    @Override
    public List<QuestionType> listQuestionType() {
        return questionMapper.listQuestionType();
    }

    @Override
    public QuestionDTO getById(Integer id) {
        return questionMapper.getById(id);
    }

    @Override
    public void edit(QuestionDTO questionDTO) {
        questionMapper.edit(questionDTO);
    }

    @Override
    public void insert(QuestionDTO questionDTO) {
        questionMapper.insert(questionDTO);
    }

    @Override
    public void deleteById(Integer id) {
        questionMapper.deleteById(id);
    }

    @Override
    public List<QuestionDTO> listAll() {
        return questionMapper.listALl();
    }


}
